package com.wenx.ftpcamel;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.model.ModelCamelContext;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;


/**
 * Created by wenx on 2019-07-22 10:07
 */
public class FileTest3 {

    public static void main(String[] args) throws Exception {
        ModelCamelContext camelContext = new DefaultCamelContext();

        camelContext.start();

        camelContext.addRoutes(new RouteBuilder() {
            @Override
            public void configure() {
                System.out.println("start time= "+LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                from("sftp://10.10.3.162:49622/data/test?username=root&password=bigdata*123" +
                        "&fileName=aa.txt" +
                        "&delete=false" +
                        "&move=./history" +
                        "&moveFailed=./error" +
                        "&binary=true" +
                        "&useUserKnownHostsFile=false" +
                        "")
                       .doTry()
                        .to("file:D:\\test")
                        .process(new Processor() {
                            public void process(Exchange exchange) throws Exception {
                                System.out.println("*******************");
                                System.out.println("文件名 ： " + exchange.getIn().getHeader(Exchange.FILE_NAME));
                                System.out.println("绝对路径 ： " + exchange.getIn().getHeader("CamelFileAbsolutePath"));
                                System.out.println("最后修改时间 ： " + exchange.getIn().getHeader("CamelFileLastModified"));
                                System.out.println("文件大小： " + exchange.getIn().getHeader("CamelFileLength"));
                                System.out.println("finish = "+LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                            }
                        })
                        .doCatch(Exception.class)
                        .process(new Processor() {
                            @Override
                            public void process(Exchange exchange) throws Exception {
                                Object object = exchange.getProperty("CamelExceptionCaught");
                                System.out.println("object = " + object);
                            }
                        });
            }
        });

        synchronized (FileDemo1.class) {
            FileDemo1.class.wait();
        }
    }

}
